
#include <vector>

#include <opencv2/opencv.hpp>
using namespace std;


FaceX* CreateFacePointsEngine(std::string modelPath)
{
	FaceX* engine = new FaceX(modelPath);
	return engine;
}

int CalculateFacePoints(FaceX* engine, unsigned char* imgBgr24, int width,
	int height, int widthstep, FaceFeatureRectangle &faceRect, FacePointsModel* pointModel)
{
	int landmarks_count = 0;
	if (engine)
	{
		cv::Mat mat(height, width, CV_8UC3, imgBgr24, widthstep);
		if (!mat.empty())
		{
			cv::Mat greyMat;
			cv::cvtColor(mat, greyMat, CV_BGR2GRAY);
			landmarks_count = engine->landmarks_count();
			cv::Rect face;
			face.x = faceRect.left;
			face.y = faceRect.top;
			face.width = faceRect.right - faceRect.left;
			face.height = faceRect.bottom - faceRect.top;
			vector<cv::Point2d> landmarks = engine->Alignment(greyMat, face);
			landmarks_count = landmarks_count <= POINT_SIZE ? landmarks_count : POINT_SIZE;
			int index = 0;

			//for (vector<cv::Point2d>::iterator landmark = landmarks.begin(); landmark != landmarks.end(); landmark++)
			//{
			//	if (index >= landmarks_count)
			//	{
			//		break;
			//	}
			//	pointModel[0].points[index].x = landmark.;
			//	pointModel[0].points[index].y = landmark.y;
			//	index++;
			//}

			for (int i = 0; i < landmarks_count; i++)
			{
				pointModel[0].points[i].x = landmarks[i].x;
				pointModel[0].points[i].y = landmarks[i].y;
			}

			//for (cv::Point2d landmark : landmarks)
			//{
			//	if (index>= landmarks_count)
			//	{
			//		break;
			//	}
			//	pointModel[0].points[index].x = landmark.x;
			//	pointModel[0].points[index].y = landmark.y;
			//	index++;
			//}
		}

	}
	return landmarks_count;
}


void Destory(FaceX* engine)
{
	if (engine)
	{
		delete engine;
		engine = NULL;
	}
}
